<?php 
class Cart
{
	function Cart()
	{
		$this->holder['js']	= '';
		$this->cartID = $this->cartID();
	}
	function getItemsFromDB()
	{
		return;
	}
	function cartID()
	{
		global $database, $s;
		$queryCartID = "SELECT id,id_user,SESSION FROM cart WHERE SESSION = '".SESSION_ID()."' OR (id_user = '".$s->userid."' AND id_user != 0) LIMIT 1";
		#SELECT id,id_user,SESSION FROM cart WHERE SESSION = '123' OR (id_user = 1 AND id_user != 0)
		$CartID = mysql_query($queryCartID, $database->connection);
		$id = mysql_fetch_array($CartID);
		$cart_id = $id['id'];
		if(mysql_num_rows($CartID) == 0)
		{
			$queryAddUserToCart = "INSERT INTO cart (id,id_user,SESSION) VALUES (NULL,'".$s->userid."', '".SESSION_ID()."')";
			$AddUserToCart = mysql_query($queryAddUserToCart, $database->connection);
			$cart_id = mysql_insert_id();
		}
		return $cart_id;	
	}
	function productInCart($id_product)
	{
		global $database, $s;
		$queryProductInCart = "SELECT antal FROM cart_produkt WHERE artnr = '".$id_product."' AND id_cart = '".$this->cartID."' LIMIT 1";
		$ProductInCart = mysql_query($queryProductInCart, $database->connection);
		$antal = mysql_fetch_array($ProductInCart);
		$product_antal = $antal['antal'];
		return $product_antal;	
	}
	function productExist($id_product)
	{
		global $database, $s;
		$queryProductExist = "SELECT artnr FROM produkt WHERE artnr = '".$id_product."' LIMIT 1";
		$ProductExist = mysql_query($queryProductExist, $database->connection);
		$artnr = mysql_fetch_array($ProductExist);
		$product_exist = $artnr['artnr'];
		if ( !$product_exist ){
			$queryProductExist = "SELECT artnr FROM u_produkt WHERE artnr = '".$id_product."' LIMIT 1";
			$ProductExist = mysql_query($queryProductExist, $database->connection);
			$artnr = mysql_fetch_array($ProductExist);
			$product_exist = $artnr['artnr'];		
		}
		if ( !$product_exist ){ $product_exist = 0; }
		return $product_exist;	
	}
	function addToCart($product_id,$qty=1)
	{
		global $database, $s;
		if ( !$this->productExist( $product_id ) ){ return 0; }
		$inCart = $this->productInCart($product_id);
		if($inCart)
		{
			$queryAddToCart = "UPDATE cart_produkt SET antal = '".($inCart+$qty)."' WHERE artnr = '".$product_id."' AND id_cart = '".$this->cartID."'";
		}
		elseif(!$inCart)
		{
			$queryAddToCart = "INSERT INTO cart_produkt (id_cart, artnr, antal, time) VALUES ('".$this->cartID."', '".$product_id."', '".$qty."', '".time()."')";
		}	
		mysql_query($queryAddToCart, $database->connection);
		return $this->createJS($product_id);
	}
	function removeFromCart($product_id,$qty=1)
	{
		global $database, $s;
		$inCart = $this->productInCart($product_id);
		$newInCart = $inCart - $qty;
		if($newInCart >= 1)
		{
			$queryRemoveFromCart = "UPDATE cart_produkt SET antal = '".($inCart-$qty)."' WHERE artnr = '".$product_id."' AND id_cart = '".$this->cartID."'";
		}
		else
		{
			$queryRemoveFromCart = "DELETE FROM cart_produkt WHERE artnr = '".$product_id."' AND id_cart = '".$this->cartID."'";
		}
		mysql_query($queryRemoveFromCart, $database->connection);
		return $this->createJS($product_id);
	}
	function createJS($product_id = '*')
	{
		global $database, $s;
		$queryCartProduct = "
			SELECT
				cart_produkt.artnr,
				produkt.pris,
				cart_produkt.antal,
				produkt.namn
			FROM cart_produkt 
			LEFT JOIN produkt ON cart_produkt.artnr = produkt.artnr
			WHERE cart_produkt.id_cart = '".$this->cartID."'
			AND produkt.typ = 1"; #maskin
		if($product_id != '*')
		{
			$queryCartProduct .= " AND cart_produkt.artnr = '".$product_id."'";
		}
		$cartProduct1 = mysql_query($queryCartProduct, $database->connection);
		
			$queryCartProduct = "
				SELECT
					cart_produkt.artnr,
					u_produkt.pris,
					cart_produkt.antal,
					CONCAT(produkt.namn,' ',u_produkt.diam,'mm x ',u_produkt.hole,'mm ') AS namn
				FROM cart_produkt 
				LEFT JOIN u_produkt ON cart_produkt.artnr = u_produkt.artnr
				LEFT JOIN produkt ON u_produkt.id_produkt = produkt.id
				WHERE cart_produkt.id_cart = '".$this->cartID."'
				AND produkt.typ != 1"; #klinga
			if($product_id != '*')
			{
				$queryCartProduct .= " AND cart_produkt.artnr = '".$product_id."'";
			}
			$cartProduct2 = mysql_query($queryCartProduct, $database->connection);
		while($res = mysql_fetch_array($cartProduct1))
		{
			$tempJS .= ''.
			$res['artnr'].':'.
			$res['pris'].':'.
			$res['antal'].':'.
			$res['namn'].'|';
		}
		while($res = mysql_fetch_array($cartProduct2))
		{
			$tempJS .= ''.
			$res['artnr'].':'.
			$res['pris'].':'.
			$res['antal'].':'.
			$res['namn'].'|';
		}
		if($product_id != '*')
		{
			return $tempJS;
		}
		$this->holder['js'] = $tempJS;
	}
	function echoJS()
	{
		$alljs = "\n<script type=\"text/javascript\">
		var cart = '';
		//cart += 'id:pris:antal:namn|';
		cart =  \"%s\"
		</script>\n";
		return sprintf($alljs,$this->holder['js']);
	}
}
?>